<!--
 * @Descripttion: (报警记录/cks_alarm_record)
 * @Author: (admin)
 * @Date: (2025-07-02)
-->
<template>
    <div>
        <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>

            <el-form-item label="" prop="deviceName">
                <el-input v-model="queryParams.deviceName" placeholder="设备名称" clearable style="width: 240px" />
            </el-form-item>
            <el-form-item label="时间">
                <!--  :default-time="defaultTime2" -->
                <el-date-picker v-model="queryParams.dateRange" style="width: 360px" type="datetimerange"
                    :default-time="defaultTime2" value-format="YYYY-MM-DD HH:mm:ss" range-separator="-"
                    start-placeholder="" end-placeholder=""></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
                <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
            </el-form-item>
        </el-form>
        <!-- 工具区域 -->
        <el-row :gutter="15" class="mb10">
            <el-col :span="1.5">
                <!-- <el-button type="primary" v-hasPermi="['cksalarmrecord:add']" plain icon="plus" @click="handleAdd">
            {{ $t('btn.add') }}
          </el-button> -->
            </el-col>
            <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
        </el-row>

        <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
            highlight-current-row @sort-change="sortChange">

            <el-table-column prop="deviceName" label="设备名称" align="center" :show-overflow-tooltip="true" />
            <el-table-column prop="signalAddress" label="报警地址" align="center" width="100"
                :show-overflow-tooltip="true" />

            <el-table-column prop="alarmCode" label="报警代码" width="160" align="center" :show-overflow-tooltip="true" />
            <el-table-column prop="faultCase" label="故障原因" align="center" :show-overflow-tooltip="true" />
            <el-table-column prop="solution" label="解决方法" align="center" :show-overflow-tooltip="true" />
            <el-table-column prop="startTime" label="开始时间" width="160" :show-overflow-tooltip="true" />
            <el-table-column prop="endTime" label="结束时间" width="160" :show-overflow-tooltip="true" />

            <!-- <el-table-column label="操作" width="160">
          <template #default="scope">
            <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['cksalarmrecord:edit']" @click="handleUpdate(scope.row)"></el-button>
            <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['cksalarmrecord:delete']" @click="handleDelete(scope.row)"></el-button>
          </template>
</el-table-column> -->
        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
            @pagination="getList" />


        <!-- 添加或修改报警记录对话框 -->
        <el-dialog :title="title" :lock-scroll="false" v-model="open">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-row :gutter="20">

                    <el-col :lg="12" v-if="opertype != 1">
                        <el-form-item label="Id" prop="id">
                            <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id"
                                :disabled="true" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="设备名称" prop="deviceName">
                            <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="报警地址" prop="signalAddress">
                            <el-input v-model="form.signalAddress" placeholder="请输入报警地址" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="开始时间" prop="startTime">
                            <el-date-picker v-model="form.startTime" type="datetime" :teleported="false"
                                placeholder="选择日期时间"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="结束时间" prop="endTime">
                            <el-date-picker v-model="form.endTime" type="datetime" :teleported="false"
                                placeholder="选择日期时间"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="报警代码" prop="alarmCode">
                            <el-input v-model="form.alarmCode" placeholder="请输入报警代码" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="故障原因" prop="faultCase">
                            <el-input v-model="form.faultCase" placeholder="请输入故障原因" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="解决方法" prop="solution">
                            <el-input v-model="form.solution" placeholder="请输入解决方法" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="状态 1报警中 2报警已消除" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio v-for="item in options.statusOptions" :key="item.dictValue"
                                    :value="parseInt(item.dictValue)">
                                    {{ item.dictLabel }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="禁用状态" prop="nullity">
                            <el-input v-model="form.nullity" placeholder="请输入禁用状态" />
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="创建时间" prop="createdTime">
                            <el-date-picker v-model="form.createdTime" type="datetime" :teleported="false"
                                placeholder="选择日期时间"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :lg="12">
                        <el-form-item label="是否删除" prop="isDeleted">
                            <el-radio-group v-model="form.isDeleted">
                                <el-radio v-for="item in options.isDeletedOptions" :key="item.dictValue"
                                    :value="item.dictValue">
                                    {{ item.dictLabel }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer v-if="opertype != 3">
                <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
                <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="cksalarmrecord">
import {
    listCksAlarmRecord,
    addCksAlarmRecord, delCksAlarmRecord,
    updateCksAlarmRecord, getCksAlarmRecord,
}
    from '@/api/business/cksalarmrecord.js'
import dayjs from 'dayjs'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const today = dayjs()
const startDay  = dayjs().subtract(3, 'day')

const defaultDateRange = [startDay.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
today.endOf('day').format('YYYY-MM-DD HH:mm:ss')]
const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    sort: '',
    sortType: 'asc',
    dateRange: defaultDateRange,
    deviceName: '',
    coverCode: ''
})
const defaultTime2 = [
    new Date(2000, 1, 1, 0, 0, 0),
    new Date(2000, 2, 1, 23, 59, 59),
] // '12:00:00', '08:00:00'

const columns = ref([
    { visible: true, prop: 'id', label: 'Id' },
    { visible: true, prop: 'deviceName', label: '设备名称' },
    { visible: true, prop: 'signalAddress', label: '报警地址' },
    { visible: true, prop: 'startTime', label: '开始时间' },
    { visible: true, prop: 'endTime', label: '结束时间' },
    { visible: true, prop: 'alarmCode', label: '报警代码' },
    { visible: true, prop: 'faultCase', label: '故障原因' },
    { visible: true, prop: 'solution', label: '解决方法' },
    { visible: false, prop: 'status', label: '状态 1报警中 2报警已消除' },
    { visible: false, prop: 'nullity', label: '禁用状态' },
    { visible: false, prop: 'createdTime', label: '创建时间' },
    { visible: false, prop: 'isDeleted', label: '是否删除' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]

function getQueryBody() {
    const { dateRange, pageNum, pageSize,deviceName } = queryParams
    const body = { pageNum, pageSize, deviceName }
    proxy.addDateRange(body, dateRange)
    return body
}

function getList() {
    loading.value = true
    const body = getQueryBody()
    listCksAlarmRecord(body).then(res => {
        const { code, data } = res
        if (code == 200) {
            dataList.value = data.result
            total.value = data.totalNum
            loading.value = false
        }
    })
}

// 查询
function handleQuery() {
    queryParams.pageNum = 1
    getList()
}

// 重置查询操作
function resetQuery() {
    proxy.resetForm("queryRef")
    queryParams.dateRange = []
    const date = dayjs()
queryParams.dateRange = defaultDateRange
    handleQuery()
}
// 自定义排序
function sortChange(column) {
    var sort = undefined
    var sortType = undefined

    if (column.prop != null && column.order != null) {
        sort = column.prop
        sortType = column.order

    }
    queryParams.sort = sort
    queryParams.sortType = sortType
    handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
    single: true,
    multiple: true,
    form: {},
    rules: {
        deviceName: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
        signalAddress: [{ required: true, message: "报警地址不能为空", trigger: "blur" }],
        alarmCode: [{ required: true, message: "报警代码不能为空", trigger: "blur" }],
        faultCase: [{ required: true, message: "故障原因不能为空", trigger: "blur" }],
        solution: [{ required: true, message: "解决方法不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态 1报警中 2报警已消除不能为空", trigger: "blur", type: "number" }],
        nullity: [{ required: true, message: "禁用状态不能为空", trigger: "blur" }],
        createdTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
        isDeleted: [{ required: true, message: "是否删除不能为空", trigger: "blur" }],
    },
    options: {
        // 状态 1报警中 2报警已消除 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
        statusOptions: [],
    }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
    open.value = false
    reset()
}

// 重置表单
function reset() {
    form.value = {
        id: null,
        deviceName: null,
        signalAddress: null,
        startTime: null,
        endTime: null,
        alarmCode: null,
        faultCase: null,
        solution: null,
        status: null,
        nullity: null,
        createdTime: null,
        isDeleted: null,
    };
    proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
    reset();
    open.value = true
    title.value = '添加报警记录'
    opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
    reset()
    const id = row.id || ids.value
    getCksAlarmRecord(id).then((res) => {
        const { code, data } = res
        if (code == 200) {
            open.value = true
            title.value = '修改报警记录'
            opertype.value = 2

            form.value = {
                ...data,
            }
        }
    })
}

// 添加&修改 表单提交
function submitForm() {
    proxy.$refs["formRef"].validate((valid) => {
        if (valid) {

            if (form.value.id != undefined && opertype.value === 2) {
                updateCksAlarmRecord(form.value).then((res) => {
                    proxy.$modal.msgSuccess("修改成功")
                    open.value = false
                    getList()
                })
            } else {
                addCksAlarmRecord(form.value).then((res) => {
                    proxy.$modal.msgSuccess("新增成功")
                    open.value = false
                    getList()
                })
            }
        }
    })
}

// 删除按钮操作
function handleDelete(row) {
    const Ids = row.id || ids.value

    proxy
        .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
            confirmButtonText: proxy.$t('common.ok'),
            cancelButtonText: proxy.$t('common.cancel'),
            type: "warning",
        })
        .then(function () {
            return delCksAlarmRecord(Ids)
        })
        .then(() => {
            getList()
            proxy.$modal.msgSuccess("删除成功")
        })
}





handleQuery()
</script>